package com.example.conference.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.conference.SaToken.StpInterfaceImpl;
import com.example.conference.domain.User;
import com.example.conference.mapper.UserMapper;
import com.example.conference.utils.JwtUtil;
import com.example.conference.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/my/")
public class MyController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    StpInterfaceImpl stpInterface;
    // 测试登录，浏览器访问： http://localhost:8088/my/doLogin?userId=1001&password=123456
    @RequestMapping("doLogin")
    public String doLogin(@RequestBody User u) {
        if (userMapper.checklogin(u) != null) {
            StpUtil.login(u.getUserId());
            List list  = stpInterface.getPermissionList(u.getUserId(),"Integer");

            System.out.println("list = " + list);
            System.out.println("boolean: " + StpUtil.hasPermission("user.add"));
            String token = StpUtil.getTokenValue();

            return token;
        } else {
            return "账号或密码错误,请重试";
        }
    }

    // 查询登录状态，浏览器访问： http://localhost:8088/my/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
    // 查询登录状态，浏览器访问： http://localhost:8088/my/isLogin
    @RequestMapping("logout")
    public String logout() {
        StpUtil.logout();
        return "注销成功";
    }

}